function renderCategory() {
    $.ajax({
        type: 'GET',
        url: '/my/category/list',
        success: function (res) {
            console.log(res)
            if (res.status === 0) {
                let str = '';
                res.data.forEach(item => {
                    str += (`<tr>
                    <td>${item.name}</td>
                    <td>${item.alias}</td>
                    <td>
                        <button data-id="${item.id}" data-name="${item.name}" data-alias="${item.alias}" type="button" class="layui-btn layui-btn-xs">编辑</button>
                        <button data-id="${item.id}" type="button" class="del layui-btn layui-btn-xs layui-btn-danger">删除</button>
                    </td>
                </tr>`);
                })
                $('tbody').html(str)
            }
        }
    })

}
renderCategory()
$('tbody').on('click', '.del', function () {
    /* let id = $(this).data('id');
    console.log(id)
    $.ajax({
        type: "GET",
        url: '/my/category/delete',
        data: {
            id: id
        },
        success: function (res) {
            console.log(res)

        }

    }) */
    let id = $(this).data('id');
    console.log(id)

    layer.confirm('你确定要删除了吗？', function (index) {

        $.ajax({
            url: '/my/category/delete',
            data: {
                id: id
            },
            success: function (res) {

                layer.msg(res.message);
                if (res.status === 0) {
                    renderCategory();
                }
            }
        });

        layer.close(index);
    });
})
let addIndex
$('button:contains("添加类别")').on('click', function (e) {
    let txt = $('#tpl-add').html()
    addIndex = layer.open({
        type: 1,
        title: "添加分类",
        content: txt //这里content是一个普通的String
    });

})
$('body').on('submit', '#add-form', function (e) {
    e.preventDefault()
    let data = $(this).serialize()
    $.ajax({
        type: "POST",
        url: '/my/category/add',
        data: data,
        success: function (res) {
            if (res.status === 0) {
                console.log(res)

                renderCategory()
                layer.close(addIndex)
            }
        }
    })
})
let editIndex
$('tbody').on('click', 'button:contains("编辑")', function () {
    let neirong = $('#tpl-edit').html()
    let val = $(this).data()
    editIndex = layer.open({
        type: 1,
        title: "添加分类",
        content: neirong,
        area: ['500px', '300px'],
        success: function () {
            let form = layui.form
            form.val('edit', val)
        }
    });
})
$('body').on('submit', '#edit-form', function (e) {
    e.preventDefault()
    let data = $(this).serialize()
    console.log(data)
    $.ajax({
        type: "POST",
        url: '/my/category/update',
        data: data,
        success: function (res) {
            layer.msg(res.message);
            if (res.status === 0) {
                console.log(res)

                renderCategory()
                layer.close(editIndex)
            }
        }
    })
})